/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


myApp.controller('viewMealDetailsController',
    function($scope, $location, viewMealDetailsService, currentlyTrendingService, $routeParams, $cookies, $rootScope) {
        $rootScope.showSearchBar = true;
        $rootScope.showSearchBar = true;
        $scope.meal = {};
        $scope.currentlyTrending = {};
        $scope.simillarDishes = {};
        $scope.mostPopularDishes = {};
        $scope.select = {};
        $scope.select.selectQuantityValue = 1;
        $rootScope.isLoginPage = true;
        var getMealSuccess = function(respData) {
            $scope.quantityArr = [];
            for(var index = 1; index <= respData.data.availableQuantity; index++){
                $scope.quantityArr.push(index);
            }
            $scope.meal = respData.data;
        }
        var getMealFailure = function(errordata) {
        }
     
        viewMealDetailsService.getMeals($routeParams.mealId).then(getMealSuccess, getMealFailure);
    
    
        currentlyTrendingService.getCurrentlyTrending($routeParams.mealId).then(function(respData){
            //success
            $scope.currentlyTrending = respData.data;
        }, getMealFailure);
    
        currentlyTrendingService.getSimilarDisheshFromOtherChefs($routeParams.mealId).then(function(respData){
            //success
            $scope.simillarDishes = respData.data;
        }, getMealFailure);
    
        currentlyTrendingService.getMostPopularDishesFromChef($routeParams.mealId).then(function(respData){
            //success
            $scope.mostPopularDishes = respData.data;
        }, getMealFailure);
    
    
        $scope.addToCart = function(mealId, mealQuantity){
            //add meal to cart cookie
            var order = {
                "mealId": mealId,
                "quantity": mealQuantity
            };
        
        
            var orders;        
            if($cookies.mealOrders){
                orders = JSON.parse($cookies.mealOrders);
            }        
        
            if(! orders){
                orders = [];
            }
            orders.push(order);
            $cookies.mealOrders = JSON.stringify(orders);
        
            $rootScope.showAlert("Meal has been added to Cart.");
            $location.path("/viewMeal" );
        };
    
    });